package cn.eweb.ttkt;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.Toast;

public class AboutActivity extends Activity {
	private ProgressDialog pd;
	private boolean isExternal = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        
        Intent intent = getIntent();
        Bundle bd = intent.getBundleExtra("bd");
        if(bd!=null){
        	isExternal = true;
        }
        
        Button goto_wls = (Button)findViewById(R.id.btn_goto_wls);
        TableRow tr_feature = (TableRow)findViewById(R.id.tr_feature);
        TableRow tr_check_new_version = (TableRow)findViewById(R.id.tr_check_new_version);
        
        goto_wls.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent( Intent.ACTION_VIEW );
        	    it.setData( Uri.parse( getString(R.string.copyright_url)) );
        	    it = Intent.createChooser( it, null );
        	    startActivity( it );
				
			}
		});
        
        tr_check_new_version.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pd = new ProgressDialog(AboutActivity.this);
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pd.setMessage("Loading");
                pd.show();
                
                //创建一个线程,每秒步长为5增加,到100%时停止  
                Thread mThread = new Thread(new Runnable() {  
                      
                    public void run() {  
                          
                    	try{  
                    		Thread.sleep(1000);
                        	Message msg = new Message();  
                            msg.what = 1;  
                            mHandler.sendMessage(msg);  
                        }catch (Exception e) {  
                            e.printStackTrace();  
                        }  
                          
                    }  
                });  
                mThread.start();  
				
			}
		});
        
        tr_feature.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveShutCutSetting("1");
				addShotCut();
				Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.feature_ok), Toast.LENGTH_SHORT);//提示框
				toast.show();
			}
		});
    }
    
    //定义一个Handler  
    private Handler mHandler = new Handler(){  
        public void handleMessage(Message msg){  
        	this.dismissDialog();
            Thread.currentThread().interrupt();  
            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.no_new_version), Toast.LENGTH_SHORT);//提示框
			toast.show();
        }
        
        private void dismissDialog() {
            if (pd != null) {
                pd.dismiss();
                pd = null;
            }
        }
    };
    
    private void addShotCut(){
		Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
		//不允许重复创建  
	    shortcutIntent.putExtra("duplicate", false);  
		//添加名称
	    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
		
	    //快捷图片  
	    Parcelable icon = Intent.ShortcutIconResource.fromContext(  
	            getApplicationContext(), R.drawable.icon);
	    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
	    
	    Intent intent = new Intent(getApplicationContext(),SplashActivity.class);
	    //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除  
        intent.setAction("android.intent.action.MAIN");  
        intent.addCategory("android.intent.category.LAUNCHER");  
	    //点击快捷图片，运行的程序主入口  
	    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		sendBroadcast(shortcutIntent);
	}
    
    private void saveShutCutSetting(String result){
		SharedPreferences sp = getSharedPreferences("setting",Context.MODE_PRIVATE);
		Editor ed = sp.edit();
		ed.putString("SHOTCUT", result);
		ed.commit();
	}
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(!isExternal){
    		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
    			AlertDialog.Builder ad = new AlertDialog.Builder(this);
            	ad.setIcon(android.R.drawable.ic_dialog_info);
        		ad.setTitle(getString(R.string.quit)); // 设置标题 
                ad.setMessage(getString(R.string.quit_confirm));
                ad.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

        			public void onClick(DialogInterface dialog, int which) {
        				dialog.cancel();
        				AboutActivity.this.finish();
        			} 
                });  
                ad.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

        			public void onClick(DialogInterface dialog, int which) {
        				dialog.cancel();
        			}
                       
                });  
                AlertDialog alert = ad.create();
                alert.show();
            }
    		return false;
    	}else{
    		return super.onKeyDown(keyCode, event);
    	}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(!isExternal){
			/*
	         * 
	         * add()方法的四个参数，依次是：
	         * 1、组别，如果不分组的话就写Menu.NONE,
	         * 2、Id，这个很重要，Android根据这个Id来确定不同的菜单
	         * 3、顺序，那个菜单现在在前面由这个参数的大小决定
	         * 4、文本，菜单的显示文本
	         */
	        menu.add(Menu.NONE, Menu.FIRST + 1, 1, getString(R.string.quit)).setIcon(
	        		android.R.drawable.ic_menu_revert);
	        return true;
		}else{
			return super.onCreateOptionsMenu(menu);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(!isExternal){
			switch (item.getItemId()) {
	        case Menu.FIRST + 1:
	        	AboutActivity.this.finish();
	            break;
			}
			return false;
		}else{
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

}
